package cn.itcast.test1;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;

public class Lambda {
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add(i);
        }

        //1、collect(toList()) & filter
        List<Integer> result1 = list.stream().filter((value) -> value > 5).collect(toList());
        result1.forEach((value) -> System.out.println(value));

        System.out.println("#####");

        //2、map
        List<String> result2 = list.stream().map((value) -> String.format("String:%s", value)).collect(toList());
        result2.forEach(value -> System.out.println(value));

        System.out.println("#####");

        //3、max&min
        Comparator<Integer> comparator = (o1, o2) -> o1.compareTo(o2);
        System.out.println(list.stream().min(comparator).get());
        System.out.println(list.stream().max(comparator).get());

        System.out.println("#####");

        //4、reduce
        int count = list.stream().reduce(0, (acc, item) -> acc + item).intValue();
        System.out.println(count);


    }
}
